<template>
  <div>
  <a-card :bordered="false" class="mb10 searchCard">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :md="6" :sm="12">
            <a-form-item label="表单名称">
              <j-input
                placeholder="输入表单名称"
                v-model="queryParam.name"
              ></j-input>
            </a-form-item>
          </a-col>

          <a-col :md="6" :sm="12">
            <a-form-item label="对应表">
                <j-input
                    placeholder="输入对应表"
                    v-model="queryParam.tableName"
                ></j-input>
            </a-form-item>
          </a-col>

          <a-col :md="6" :sm="8">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
  </a-card>
  <a-card :bordered="false" class="mb10 searchCard">
  <!-- 操作按钮区域 -->
    <div class="table-operator" style="border-top: 5px;">
      <a-button @click="handleAdd" type="primary" icon="plus"
        >新增表单</a-button
      >
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay" @click="handleMenuClick">
          <a-menu-item key="1">
            <a-icon type="delete" @click="batchDel" />
            删除
          </a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px">
          批量操作
          <a-icon type="down" />
        </a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px">
        <i class="anticon anticon-info-circle ant-alert-icon"></i>已选择&nbsp;<a
          style="font-weight: 600"
          >{{ selectedRowKeys.length }}</a
        >项&nbsp;&nbsp;
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{
          selectedRowKeys: selectedRowKeys,
          onChange: onSelectChange,
        }"
        @change="handleTableChange"
      >
        <template slot="avatarslot" slot-scope="text, record">
          <div class="anty-img-wrap">
            <a-avatar shape="square" :src="getAvatarView(record.avatar)" icon="user"/>
          </div>
        </template>
        <span slot="publishState" slot-scope="text, record">
            <a-tag color="green" v-if="text==1">已发布</a-tag>
            <a-tag color="red" v-else >未发布</a-tag>
        </span>
        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>
          <a-divider type="vertical" />
          <a @click="formDesign(record)">设计表单</a>
          <a-divider type="vertical" />
          <a @click="formView(record)">表单预览</a>
          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link"> 更多 <a-icon type="down"/> </a>
            <a-menu slot="overlay">
              <!--<a-menu-item>
                <a @click="routerGet(record)">表单详情</a>
              </a-menu-item>-->
              <a-menu-item>
                <a href="javascript:;" @click="formCopy(record)">复制表单</a>
              </a-menu-item>
              <a-menu-item>
                <a href="javascript:;" @click="handleDetail(record)">详情</a>
              </a-menu-item>
              <a-menu-item v-if="record.publishState != 1">
                  <a-popconfirm title="确定发布吗?" @confirm="() => handlePublish(record.id)">
                      <a>发布</a>
                  </a-popconfirm>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
      </a-table>
    </div>
      <!-- table区域-end -->
      <form-modal ref="modalForm" @ok="modalFormOk"></form-modal>
      <form-design ref="formDesign" @ok="modalFormOk"></form-design>
      <form-view ref="formView" @callback="callback"></form-view>
      <form-copy ref="formCopy" @ok="modalFormOk"></form-copy>
  </a-card>
  </div>
</template>
<script>
import { getFileAccessHttpUrl } from "@/api/manage";
import { KsListMixin } from '@/mixins/KsListMixin'
import JInput from '@/components/ksource/JInput'
import {request, METHOD, removeAuthorization} from '@/utils/request'
import FormView from './modules/FormView'
import FormModal from "./modules/FormModal";
import FormDesign from './modules/FormDesign'
import FormCopy from "./modules/FormCopy";
import {publishBpmForm} from '@/api/api'

export default {
  name: "FormList",
  mixins: [KsListMixin],
  components: {
      JInput,
      FormView,
      FormModal,
      FormDesign,
      FormCopy
  },
  data() {
    return {
    FormViewModel: false,
    refreshView:true,
    formId:0,
      description: "管理页面",
            currentDeptId: '',
      queryParam: {},
      recycleBinVisible: false,
      columns: [
          {
              title: '表单名称',
              align: "left",
              dataIndex: 'name',
              width: 120,
              sorter: true
          },
          {
              title: '表单类型',
              align: "left",
              width: 100,
              dataIndex: 'type',
              sorter: true,
              customRender: (text) => {
                switch (text) {
                  case 'dblx':
                    return "单表类型";
                  case 'zclx':
                    return "主从类型";
                  default:
                    return "单表类型";
                }
              }
          },
          {
            title: '主表',
            align: "left",
            width: 100,
            dataIndex: 'tableName'
          },
          {
            title: '附表',
            width: 100,
            dataIndex: 'assistTableName'
          },
          {
              title: '发布状态',
              align: "left",
              width: 100,
              dataIndex: 'publishState',
              scopedSlots: {customRender: 'publishState'},
              sorter: true,
              /*customRender: (text) => {
                  switch (text) {
                      case 1:
                          return "已发布";
                      default:
                          return "未发布";
                  }
              }*/
          },
          {
              title: '操作',
              dataIndex: 'action',
              scopedSlots: {customRender: 'action'},
              align: "center",
              width: 170
          }
      ],
      url: {
          list: "/base/bpm/form/list",
          delete: "/base/bpm/form/delete",
          deleteBatch: "/base/bpm/form/deleteBatch",
      },
    };
  },
  methods: {
    getAvatarView: function (avatar) {
      return getFileAccessHttpUrl(avatar);
    },
    handleMenuClick(e) {
      if (e.key == 1) {
        this.batchDel();
      }
    },
    formDesign: function (record) {
      this.$refs.formDesign.edit(record);
    },
    formView: function (record) {
      //dataId不为空为修改
      var param={
        formId:record.id,
        dataId:"",
      }
      this.$refs.formView.view(param);
    },
    formCopy: function (record) {
      this.$refs.formCopy.copy(record);
      this.$refs.formCopy.title = "复制表单";
      this.$refs.formCopy.disableSubmit = false;
    },
    routerEdit(record){
      var param={
        formId:record.id,
        dataId:"",
      }
      console.log(param)
      this.$router.push({
        path:'/base/bpm/FormDataEdit',
        query: param
      })
    },
    routerGet(record){
      var param={
        formId:record.id,
        dataId:"",
      }
      console.log(param)
      this.$router.push({
        path:'/base/bpm/FormDataGet',
        query: param
      })
    },
    handlePublish(id){
        var that = this;
        publishBpmForm({
            id: id
        }).then((res) => {
            if (res.success) {
              that.$message.success(res.message);
              that.loadData();
            } else {
              that.$message.warning(res.message);
            }
        })
    },
    callback(obj){
      this.$message.success(obj);
      console.log(obj)

    },
  },
};
</script>
